 /**
 * Description: 物资出库记录
 * @author:Hunter
 * @since:1.0.0
 *
 * Modification History:
 * Date         Author      Version     Description
 * ------------------------------------------------------------------
 * 2018-04-08   Hunter       1.0.0     Initial
 */
package com.metro.paperless.app.service.impl;

import com.metro.core.dao.IMapper;
import com.metro.core.service.BaseServiceSupport;
import com.metro.core.service.ServiceException;
import com.metro.core.vo.JsonResult;
import com.metro.core.vo.ResultCode;
import com.metro.core.vo.inf.IResult;
import com.metro.paperless.app.beans.vo.AppInDetail;
import com.metro.paperless.app.beans.vo.AppOutDetail;
import com.metro.paperless.app.beans.vo.AppOutDetailForm;
import com.metro.paperless.app.mapper.IAppOutDetailMapper;
import com.metro.paperless.biz.beans.vo.BizMaterialsManage;
import com.metro.paperless.biz.mapper.IBizMaterialsManageMapper;
import com.metro.paperless.biz.service.inf.IBizMaterialsManageService;
import com.weslink.qsign.util.App;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.metro.paperless.app.mapper.IAppMaterialsOutMapper;
import com.metro.paperless.app.service.inf.IAppMaterialsOutService;
import com.metro.paperless.app.beans.vo.AppMaterialsOut;

import java.util.Date;
import java.util.List;

 /**
 * 物资出库记录<br>
 *
 * @author:Hunter
 * @since:1.0.0
 * @version [1.0.0, 2018-04-08]
 */
@Service("appmaterialsoutService")
public class AppMaterialsOutService extends BaseServiceSupport<AppMaterialsOut> implements IAppMaterialsOutService{
    //物资出库记录Mapper
    @Autowired
    private IAppMaterialsOutMapper appmaterialsoutMapper;
    @Autowired
    private IAppOutDetailMapper outDetailMapper;
    @Autowired
    private IBizMaterialsManageMapper materialsManageMapper;

    @Override
    public IMapper getMapper() {
        return appmaterialsoutMapper;
    }

    @Override
    public IResult save(AppMaterialsOut entity) {
        try {
            appmaterialsoutMapper.save(entity);
            return ResultCode.SUCCESS;
        } catch (Exception e) {
            log.error("新增物资出库记录失败", e);
            throw new ServiceException(ResultCode.ERROR);
        }
    }


     @Override
     public JsonResult save(AppOutDetailForm outDetailForm, AppMaterialsOut entity) {
         try {
             JsonResult jr = new JsonResult();
             entity.setActionTime(new Date());
             entity.setOutStatus("1");
             appmaterialsoutMapper.save(entity);
             Long outId = entity.getId();
             saveUpdateOut(outDetailForm, outId);
             jr.setData(outId);
             jr.setResult(ResultCode.SUCCESS);
             return jr;
         } catch (Exception e) {
             log.error("新增物资出库记录失败", e);
             throw new ServiceException(ResultCode.ERROR);
         }
     }

     private void saveUpdateOut(AppOutDetailForm outDetailForm, Long outId) {
         for (AppOutDetail itementity : outDetailForm.getItemOutEntity()) {
             itementity.setOutBills(outId);
             itementity.setOutStatus(1);
             outDetailMapper.save(itementity);
             Long matId = itementity.getMaterialsId();
             BizMaterialsManage materialsManage = materialsManageMapper.getById(matId);
             int enableStockNum = materialsManage.getEnableStockNum();
             int outNum = itementity.getOutNum();
             enableStockNum  = enableStockNum - outNum;
             materialsManage.setEnableStockNum(enableStockNum);
             materialsManageMapper.updateNotEmpty(materialsManage);
         }
     }

     @Override
     public IResult update(AppOutDetailForm itemEntity, AppMaterialsOut entity) {
         try {
             JsonResult jr = new JsonResult();
             appmaterialsoutMapper.updateNotEmpty(entity);
             Long outId = entity.getId();
             List<AppOutDetail> outDetails = outDetailMapper.queryListByOutId(outId);
             for(AppOutDetail detail:outDetails){
                 BizMaterialsManage materialsManage = materialsManageMapper.getById(detail.getMaterialsId());
                 materialsManage.setEnableStockNum(materialsManage.getEnableStockNum()+detail.getOutNum());
                 materialsManageMapper.updateNotEmpty(materialsManage);
             }
             outDetailMapper.deleteByBillIds(entity.getId().toString());//现有的所有记录准备重新添加
             saveUpdateOut(itemEntity, outId);
             jr.setData(outId);
             jr.setResult(ResultCode.SUCCESS);
             return jr;
         } catch (Exception e) {
             log.error("新增物资出库记录失败", e);
             throw new ServiceException(ResultCode.ERROR);
         }
     }

     @Override
     public IResult update(AppMaterialsOut entity, String action) {
         try {
             appmaterialsoutMapper.updateNotEmpty(entity);
             List<AppOutDetail> outDetailList = outDetailMapper.queryListByOutId(entity.getId());
             for (AppOutDetail itementity : outDetailList) {
                 if("commint".equals(action)){
                     itementity.setOutStatus(1);
                 }else{
                     itementity.setOutStatus(3);
                 }
                 outDetailMapper.updateNotEmpty(itementity);
                 Long matId = itementity.getMaterialsId();
                 BizMaterialsManage materialsManage = materialsManageMapper.getById(matId);
                 int stockNum;
                 int enablestockNum;
                 if("commint".equals(action)){
                      stockNum = materialsManage.getStockNum()-itementity.getOutNum();
                 }else{
                     stockNum = materialsManage.getStockNum()+itementity.getOutNum();
                     enablestockNum = materialsManage.getEnableStockNum()+itementity.getOutNum();
                     materialsManage.setEnableStockNum(enablestockNum);
                 }
                 materialsManage.setStockNum(stockNum);
                 materialsManageMapper.updateNotEmpty(materialsManage);
             }

             return ResultCode.SUCCESS;
         } catch (Exception e) {
             log.error("修改物资出库记录失败", e);
             throw new ServiceException(ResultCode.ERROR);
         }
     }

     @Override
    public IResult update(AppMaterialsOut entity) {
        return null;
    }

    @Override
    public IResult delete(Long id) {
        try {
            appmaterialsoutMapper.delete(id);
            return ResultCode.SUCCESS;
        } catch (Exception e) {
            log.error("删除失败", e);
            throw new ServiceException(ResultCode.ERROR);
        }
    }

    @Override
    public IResult deleteByIds(String ids) {
        try {
            appmaterialsoutMapper.deleteByIds(ids);//删除记录表数据
            List<AppOutDetail> outlist = outDetailMapper.queryListByOutId(Long.valueOf(ids));//查询明细表当前入库数量
            for(AppOutDetail detail:outlist){
                BizMaterialsManage mat = materialsManageMapper.getById(detail.getMaterialsId());
                int enablenum = mat.getEnableStockNum();//获取物资表可用库存
                int curenablenum = enablenum + detail.getOutNum();//当前的可用库存
                mat.setEnableStockNum(curenablenum);
                materialsManageMapper.updateNotEmpty(mat);//更新资源表可用库存
            }
            outDetailMapper.deleteByBillIds(ids);//删除明细表数据
            return ResultCode.SUCCESS;
        } catch (Exception e) {
            log.error("删除失败", e);
            throw new ServiceException(ResultCode.ERROR);
        }
    }
}